package com.ecarx.vhaltests.hy11.testcases.notify;

import android.car.VehicleAreaType;
import android.car.VehiclePropertyType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import com.ecarx.vhaltests.CallbackTimer;
import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.automotive.VehicleArea;
import com.ecarx.xui.adaptapi.automotive.VehiclePropertyGroup;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import java.util.HashMap;
import java.util.Map;

import vendor.ecarx.xma.automotive.vehicle.V2_0.ECarXVehicleProperty;

/**
 * 电池电量校准 通知 425
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class NotifyInfo425Test extends VhalUnitTestBase {
    private static final String TAG = "NOTIFY_INFO";

    private static IpcpClient ipcpClient;
    private static final int AREA_ID = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;

    private static final int NOTIFY_ID = 425;
    private static final int STATUS_SHOW = 1;

    private static final Map<Integer, Integer> notifies = new HashMap<Integer, Integer>();

    private static CarPropertyManager.CarPropertyEventCallback propertyCbk = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            Integer[] values = (Integer[]) carPropertyValue.getValue();
            Log.d(TAG, "recv cb, value:[notifyId:" + values[0] + ", status:" + values[1] + "]");
            if (carPropertyValue.getPropertyId() == ECarXVehicleProperty.NOTIFY_INFO && carPropertyValue.getAreaId() == AREA_ID) {
                notifies.put(values[0].intValue(), values[1].intValue());
            }
        }

        @Override
        public void onErrorEvent(int i, int i1) {

        }
    };

    @BeforeClass
    public static void testInit() {
        setPreCondition(true);
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(ECarXVehicleProperty.NOTIFY_INFO, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        Assert.assertTrue(isAvail);

        delay();

        mCarPropertyMgr.registerCallback(propertyCbk, ECarXVehicleProperty.NOTIFY_INFO, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        delay();
    }

    @Test
    public void test001_NotifyInfo425_Show() {
        setVddmValue(1);
        delay(1000);
        if (notifies.containsKey(NOTIFY_ID)) {
            Assert.assertEquals(STATUS_SHOW, notifies.get(NOTIFY_ID).intValue());
        } else {
            Assert.assertTrue(false);
        }
    }

    private static void setPreCondition(boolean isAvail) {
        if (ipcpClient == null) {
            ipcpClient = new IpcpClient();
        }
        int vddmSvcId = 48;
        int vddmOperId = 999;
        int[] vddmOperIdIds = {999};
        int ccSvcId = 135;
        int ccOperId = 200;
        int[] ccOperIds = {200};
        int[] configs = {13};
        int[] values = {isAvail ? 4 : 1};

        ipcpClient.setConfig(vddmSvcId, vddmOperIdIds);
        ipcpClient.setConfig(ccSvcId, ccOperIds);

        boolean ccRet = ipcpClient.setCarconfigSignal(ccSvcId, ccOperId, configs, values);
        Assert.assertTrue(ccRet);
    }

    private static void setVddmValue(int value) {
        int vddmSvcId = 48;
        int vddmOperId = 999;
        String[] vddms = {"hvBattOptmzHint"};
        int[] vddmValues = {value};
        boolean vddmRet = ipcpClient.setVDDMSignal(vddmSvcId, vddmOperId, vddms, vddmValues);
        Assert.assertTrue(vddmRet);
        Log.d(TAG, "send vddm value pass");
    }
}